package cn.anc.aonicardv.module.ui.recorder;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.regex.Pattern;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.OnClick;
import cn.anc.aonicardv.Constant;
import cn.anc.aonicardv.R;
import cn.anc.aonicardv.R2;
import cn.anc.aonicardv.module.ui.MainActivity;
import cn.anc.aonicardv.module.ui.base.BaseActivity;
import cn.anc.aonicardv.util.DialogUtils;
import cn.anc.aonicardv.util.ui.UiTool;
import cn.anc.aonicardv.widget.WarningDialog;
import cn.anc.httpcontrolutil.CarControl;
import cn.anc.httpcontrolutil.CarControlSettings;

/**
 * Created by yangdai on 2017/6/22.
 */

public class DriverNumberActivity extends BaseActivity implements CarControl.OnHeartBeatCallBack, View.OnClickListener {

    @BindView(R2.id.tv_title)
    TextView titleTv;
    @BindView(R2.id.edit_driver_number)
    EditText driverNumberEdit;
    private CarControlSettings.SettingItem settingItem;
    private Dialog progressDialog;
    private WarningDialog warningDialog;
    private DialogUtils dialogUtils;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_driver_number);
        super.onCreate(savedInstanceState);

    }

    @Override
    public void initVariable() {
        String key = getIntent().getStringExtra(Constant.IntentKeyParam.SETTING_KEY);
        settingItem = CarControlSettings.getSettingItem(key);
        dialogUtils = new DialogUtils();
        progressDialog = dialogUtils.getCustomProgressDialog(this);
        warningDialog = dialogUtils.getWarningDialog(this);
    }

    @Override
    public void setListener() {
        CarControl.setOnHeartBeatCallBack(this);
        warningDialog.okTv.setOnClickListener(this);
    }

    @Override
    public void initData() {
        titleTv.setText(getString(R.string.my_driver_number_setting));
        driverNumberEdit.setText(settingItem.settingItemValue.trim());
    }

    @OnClick(R2.id.tv_back)
    public void back() {
        finish();
    }

    @OnClick(R2.id.tv_ok)
    public void ok() {
        String input = driverNumberEdit.getText().toString().trim();
        boolean b = matcheString(input);
        if (!b) {
            UiTool.showToast(DriverNumberActivity.this, R.string.my_driver_number_setting_hint);
            return;
        }
        if (input.length() < 9) {
            int t = 9 - input.length();
            for (int i = 0; i < t; i++) {
                input += " ";
            }
        }
        setDriverNumber(input);
    }

    private boolean matcheString(String input) {
        String regex = "^[a-zA-Z0-9]+$";
        Pattern pattern = Pattern.compile(regex);
        return pattern.matcher(input.replace(" ", "")).matches();
    }


    private void setDriverNumber(String s) {
        settingItem.settingItemValue = s;
        progressDialog.show();
        CarControl.doSetSettingItemValue(settingItem.settingItemKey, settingItem.settingItemValue, new CarControl.OnCarControlCallback() {
            @Override
            public void OnCommandCallback(int code, CarControl.CommandResponseInfo info) {
                progressDialog.dismiss();
                Toast.makeText(DriverNumberActivity.this, getString(R.string.my_driver_number_setting_hint2), Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }

    @Override
    public void OnHeartBeatConnectOK() {
    }

    @Override
    public void OnHeartBeatConnectFail() {
    }

    @Override
    public void OnHeartBeatDisconnect() {
        if (null != warningDialog)
            warningDialog.setHint(getString(R.string.tip_cardv_disconnect)).show();
    }

    @Override
    public void onClick(View v) {
        warningDialog.dismiss();
        startActivity(new Intent(this, MainActivity.class));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
